//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "CreateRandomClusters.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <fstream>
#include <iostream>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHLINE 5000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
CreateRandomClusters::CreateRandomClusters() {
  this->filenameList = new String();
  this->filenameSize = new String();
  this->filenameOut  = new String();
  this->locationOut  = new String();
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
CreateRandomClusters::CreateRandomClusters(const CreateRandomClusters& createRandomClusters) {
  this->filenameList = new String(createRandomClusters.filenameList);
  this->filenameSize = new String(createRandomClusters.filenameSize);
  this->filenameOut  = new String(createRandomClusters.filenameOut);
  this->locationOut  = new String(createRandomClusters.locationOut);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
CreateRandomClusters::CreateRandomClusters(const CreateRandomClusters* createRandomClusters) {
  this->filenameList = new String(createRandomClusters->filenameList);
  this->filenameSize = new String(createRandomClusters->filenameSize);
  this->filenameOut  = new String(createRandomClusters->filenameOut);
  this->locationOut  = new String(createRandomClusters->locationOut);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
CreateRandomClusters::~CreateRandomClusters() {
  if (this->filenameList) delete this->filenameList;
  if (this->filenameSize) delete this->filenameSize;
  if (this->filenameOut)  delete this->filenameOut;
  if (this->locationOut)  delete this->locationOut;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void CreateRandomClusters::FilenameList(String* filenameList) {
  if (filenameList) {
    if (this->filenameList) delete this->filenameList;
    this->filenameList = filenameList;
  }
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void CreateRandomClusters::FilenameSize(String* filenameSize) {
  if (filenameSize) {
    if (this->filenameSize) delete this->filenameSize;
    this->filenameSize = filenameSize;
  }
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void CreateRandomClusters::LocationOut(String* locationOut) {
  if (locationOut) {
    if (this->locationOut) delete this->locationOut;
    this->locationOut = locationOut;
  }
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void CreateRandomClusters::FilenameOut(String* filenameOut) {
  if (filenameOut) {
    if (this->filenameOut) delete this->filenameOut;
    this->filenameOut = filenameOut;
  }
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* CreateRandomClusters::FilenameList(void) {
  return this->filenameList;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* CreateRandomClusters::FilenameSize(void) {
  return this->filenameSize;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* CreateRandomClusters::LocationOut(void) {
  return this->locationOut;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* CreateRandomClusters::FilenameOut(void) {
  return this->filenameOut;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool CreateRandomClusters::ConstructRandomClusters() {

  if (!this->filenameList || !this->filenameSize || 
      !this->filenameOut || !this->locationOut)
    return false;

  TListE <String>* listPDB  = new TListE <String> ();
  TListE <int>*    listSize = new TListE <int>    ();
  
  fstream          filePDB, fileSize, fileOut;
  
  char             completePath[strlen(this->locationOut->Out()) + LENGTHLINE];
  char             buffer[LENGTHLINE + 1];

  filePDB.open(this->filenameList->Out(), fstream::in);
  while (!filePDB.eof()) {
    filePDB.getline(buffer, LENGTHLINE);
    String* pdb = new String(buffer);
    pdb->Trim();
    listPDB->Add(pdb);
  }
  filePDB.close();

  fileSize.open(this->filenameSize->Out(), fstream::in);
  while (!fileSize.eof()) {
    fileSize.getline(buffer, LENGTHLINE);
    listSize->Add(atoi(buffer));
  }
  fileSize.close();

  int clusterCount = 1;

  listSize->SetInitial();
  while (listSize->SetNext()) {
    sprintf(completePath, "%s%sCluster%05d.list\0", this->locationOut->Out(), this->filenameOut->Out(), clusterCount++);

    printf("%s%sCluster%05d.list\n", this->locationOut->Out(), this->filenameOut->Out(), clusterCount);
    cout << "Size: " << *(listSize->GetCurrent()) << endl;
    
    fileOut.open(completePath, fstream::out);
    for (int i = 0; i < *(listSize->GetCurrent()); i++) {
      int positionRandom = rand() % listPDB->Length();
      fileOut << listPDB->Get(positionRandom)->Out() << endl;
      listPDB->Delete(positionRandom);
    }
    fileOut.close();
  }

  if (listPDB)  delete listPDB;
  if (listSize) delete listSize;

  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
